<?php 
//商品的修改
date_default_timezone_set("PRC");
include_once("./com.php");
if(isset($_POST['sub'])){
	// 1. 获取数据
 	$data = $_POST;

 	$pid = $_POST['pid'];

 	// 2. 数据处理
 	$data['descp'] = htmlspecialchars($data['descp']);
 	$data['color'] = implode("-",$data['color']);
 	$data['size'] = implode("-",$data['size']);
 	unset($data['sub']);
 	unset($data['pid']);

 	// 商品封面图
 	$pimg = $_FILES['pimg'];
 	if(!empty($pimg['name'])){
 		 $arr = explode('.', $pimg['name']);
 		 $ext = end($arr);
 		 $fileNewName = date("Ymd").time().rand(1000,9999).".".$ext;
 		 move_uploaded_file($pimg['tmp_name'], '../uploads/'.$fileNewName);
 		 $data['pimg'] = $fileNewName;
 	}	 
 	// 商品相册
 	$pimgs = $_FILES['pimgs'];
  	if(!empty($pimgs['name'][0])){
  		for($i=0;$i<count($pimgs['name']);$i++){
  			$arr = explode('.', $pimgs['name'][$i]);
  			$ext = end($arr);
  			$fileNewName = date("Ymd").time().rand(1000,9999).".".$ext;
  			move_uploaded_file($pimgs['tmp_name'][$i],'../uploads/'. $fileNewName);
  			$info[] = $fileNewName;
  		}
  		$data['pimgs'] = implode('-', $info);
  	}
  	// print_r($data);

  	// 3. 组装sql语句并执行
  	$sql = "update product set ";
 	foreach($data as $key=>$value){
 		$sql .= "{$key}='{$value}',";
 	}
 	$sql = substr($sql, 0, -1); #去掉最后的 , 号
 	$sql .= " where pid={$pid}";
 	// echo $sql;
 	mysql_query($sql);

 	// 4. 提示
 	if(mysql_affected_rows()>0){
 		echo "<script>alert('操作成功');location.href='product.php';</script>";
 	}else{
 		echo "<script>alert('操作失败');location.href='product.php?pid=".$pid."';</script>";
 	}
 	exit;
}else{
	$pid=$_GET['pid'];
	$rem="select * from product where pid='$pid'";
	$ser=mysql_query($rem);
	$acd=mysql_fetch_assoc($ser);
	$arr=$acd['color'];
	$arre=explode("-",$arr);
	$arra=$acd['size'];
	$arrae=explode("-",$arra);
	$ifshow=$acd['ifshow'];
	$img=$acd['pimgs'];
	$imgg=explode("-",$img);
	$descpp=$acd['descp'];

	$red="select * from product";
	mysql_query($red);
	$set = "select * from pc_fenlei";
	$res = mysql_query($set);
	while($list = mysql_fetch_assoc($res)){
	$sets[] = $list;
	};

	function arrav($sets,$fid=0){
		static $arr_arr=array();
		foreach($sets as $key=>$value){
			if($value['pc_fid']==$fid){
				$arr_arr[]=$value;
				arrav($sets,$value['pc_cid']);
			}
		}
		return $arr_arr;
	}
	$arr_arr=arrav($sets);
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>主体部分</title>
<base target="_self">
<link rel="stylesheet" type="text/css" href="skin/css/base.css" />
<link rel="stylesheet" type="text/css" href="skin/css/main.css" />
<style>
	.c1{
		width:20px;
		height:20px;
		background:red;
		display:inline-block;
	}
	.c2{
		width:20px;
		height:20px;
		background:green;
		display:inline-block;
	}
	.c3{
		width:20px;
		height:20px;
		background:blue;
		display:inline-block;
	}


</style>
</head>
<body leftmargin="8" topmargin='8'>
<table width="98%" align="center" border="0" cellpadding="3" cellspacing="1" bgcolor="#CBD8AC" style="margin-bottom:8px;margin-top:8px;">
  <tr>
    <td background="skin/images/frame/wbg.gif" bgcolor="#EEF4EA" class='title' colspan="2"><span><img src='skin/images/frame/arr3.gif' style='margin-right:10px;'>修改商品</span></td>
  </tr>
  <tr bgcolor="#FFFFFF">
    <td>
		<table style="margin-bottom:8px;margin-top:8px;margin-left:20px;">
			<form  action="" method="post" enctype="multipart/form-data">
			<input type="hidden" name="pid" value="<?php echo $acd['pid'];?>">
			<tr>
				<td width="100px;">商品名称:</td>
				 <td><input type="text" name="pname" value="<?= $acd['pname']?>"></td>
			</tr>	
			<tr> 
				<td>商品分类:</td>
				<td>
					<select name="pc_cid">
					<?php foreach ($arr_arr as $value) { ?>
					<option value="<?= $value['pc_cid'] ?>">
					<?= str_repeat("&nbsp;",($value['pc_level']-1)*9).$value['pc_name']?>
					</option>
				<?php } ?>
					</select>
				</td>
			</tr>  
			<tr>
				 <td>产品价格：</td>
				 <td><input type="text" name="price" value="<?= $acd['price']?>"></td>
			</tr> 
			<tr>
				 <td>优惠价格：</td>
				 <td><input type="text" name="sprice" value="<?= $acd['sprice']?>"></td>
			</tr>  
			<tr>
				 <td>产地：</td>
				 <td><input type="text" name="addr" value="<?= $acd['addr']?>"></td>
			</tr>  
			<tr>
				 <td>关键字：</td>
				 <td><input type="text" name="keyword" value="<?= $acd['keyword']?>"></td>
			</tr> 
			<tr>
				 <td>产品序列号：</td>
				 <td><input type="text" name="pnums" value="<?= $acd['pnums']?>"></td>
			</tr> 
			<tr>
				 <td>库存：</td>
				 <td><input type="text" name="snums" value="<?= $acd['snums']?>"></td>
			</tr>  
			<tr>
				 <td>是否上架：</td>
				 <td><input type="radio" name="ifshow" value="1" <?= $acd['ifshow']==1?"checked":""?>>上架
				 	<input type="radio" name="ifshow" value="2" <?= $acd['ifshow']==2?"checked":""?>>不上架
				 </td>
			</tr> 
			<tr>
				 <td>颜色：</td>
				 <td>
				<input type="checkbox" name="color[]" value="red"
				<?php foreach ($arre as $value) { ?>
					<?=  $value=='red'?'checked':''; ?>
				<?php } ?>
					><div class="c1"></div>
				<input type="checkbox" name="color[]" value="green" <?= in_array("green",$arre)?"checked":"" ?>>
				<div class="c2"></div>
				<input type="checkbox" name="color[]" value="blue" <?= in_array("blue",$arre)?"checked" : ""?>>
				<div class="c3"></div></td>	
			<tr>
			<tr>
				 <td>尺寸 ：</td>
				<td>
				<input type="checkbox" name="size[]" value="L" <?= in_array("L",$arrae)?"checked" : ""?>>L
				<input type="checkbox" name="size[]" value="XL" <?= in_array("XL",$arrae)?"checked" : ""?>>XL
				<input type="checkbox" name="size[]" value="XXL" <?= in_array("XXL",$arrae)?"checked" : ""?>>XXL
				<input type="checkbox" name="size[]" value="XXXL" <?= in_array("XXXL",$arrae)?"checked" : ""?>>XXXL
				</td>	
			</tr>
			<tr>
				 <td>标题图片</td>
				<td>
				<input type="file" name="pimg" value=""><img src="../uploads/<?= $acd['pimg']?>" width="20px">
				</td>	
			</tr>
			<tr>
				 <td>图片库</td>
				<td>
				<input type="file" name="pimgs[]" value="" multiple>
				<?php  foreach ($imgg as $value){ 
					echo "<img src='../uploads/$value' width='20px'>";
				}?>
				</td>	
			</tr>
			<tr>
				<td>产品描述：</td>
				<td>
				<?php
				include_once("./fckeditor/fckeditor.php");
				$fckeditor = new FCKeditor("descp");//定义默认值 name
				$fckeditor->Width = "300px";//定义编辑器的宽度
				$fckeditor->Height = "200px";//定义编辑器的高度
				$fckeditor->Value = "$descpp";//定义默认值
				$fckeditor->BasePath='./fckeditor/';
				$fckeditor->ToolbarSet = "Basic";
				$fckeditor->Create();//创建编辑器
				?>
				</td>
				<tr>
				 <td colspan="2" align="center" height="40px">
				 	<input type="submit" name="sub" value=" 修改 " class="coolbt2"></td>
			   </tr>
			</tr>
		</form>
		</table>
	</td>
  </tr>
</table>
</body>
</html>